package cn.action;

import cn.utils.SuperUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.springframework.http.server.ServletServerHttpResponse;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Map;

public class RandomAction extends ActionSupport {

    public String createCode() {
        //生成随机字串
        String verifyCode = SuperUtil.generateVerifyCode(4);

        Map session = ActionContext.getContext().getSession();

        //存入会话session
        session.put("CODE", verifyCode.toLowerCase());
        int width = 160;//宽
        int height = 50;//高
        File dir = new File("E:/");
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        try {
            SuperUtil.outputImage(width, height, response.getOutputStream(), verifyCode);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
